<?php
    require "../init.php";

    if(empty($_SESSION['admin'])){
        admin_redirect("请登录",3,ADMIN_ROOT_URL . "login.php");
        die;
    }
    if(($_SESSION['admin']['type'] != 0) && ($_SESSION['admin']['type'] != 1)){
        admin_redirect('您没有权限');die;
    }

    $act = $_GET["act"];

    if($act == "edit"){

        //查看表单元素是否有空的
        if(empty($_POST['cname'])){
            admin_redirect('分类名称不能为空');
            die;
        }
        //获得修改过的分类名和该分类的id;
        $cname = $_POST['cname'];
        $id = $_POST['id'];

        //准备sql语句,查询该分类是否已存在
        $sql = "select id from ". PRE . "category where cname = '" . $cname . "' and id != $id limit 1";

        // 执行sql语句
        $result = getRow($link, $sql);

        //判断该用户是否已存在
        if($result){
            admin_redirect("该分类已存在");
            die;
        }

        //准备sql更改数据库
        $sql = "update ". PRE . "category set cname = '" . $cname ."' where id = '" . $id ."'";
        //执行sql语句
        $result = execute($link, $sql);
        if($result){
            admin_redirect("修改分类名称成功", 3, ADMIN_ROOT_URL . "category/index.php");
            die;
        }else{
            admin_redirect("修改分类名称失败");
            die;
        }
    }elseif($act == "del"){
        $type = $_SESSION['admin']['type'];
        //0是超级管理员,1是普通管理员
        if($type != 0){
            admin_redirect("您的权限不足,不能进行该操作");
            die;
        }
        //获得要删除分类的id,pid,path
        $id= $_GET['id'];
        $pid = $_GET['pid'];
        $path = $_GET['path'];
        // 顶级分类不能删除 有子类的分类不能删除 分类下有商品的不能删除
        if(($pid != 0) && ($path != "0,")){//判断是否是顶级分类
            //拼接查询条件
            $where = $path.$id.",";
            $sql = "select id from ". PRE ."category where path like '$where%' limit 1";
            $res = getRow($link, $sql);

            if($res){
                admin_redirect("此分类有子类,不能删除");
                die;
            }

            //查看此分类下是否有商品
            $sql = "select id from ".PRE."goods where cate_id = $id limit 1";
            $res = getRow($link, $sql);
            if($res){
                admin_redirect("此分类下有商品,不能删除");
                die;
            }


            //删除此分类
            $sql = "delete from ". PRE ."category where id = $id";
            $res = execute($link, $sql);
            if($res){
                admin_redirect("删除分类成功");
                die;
            }else{
                admin_redirect("删除分类失败");
                die;
            }
        }else{
            admin_redirect("这是一级分类,不能删除");
            die;
        }

    }elseif($act == "add"){

        //查看表单元素是否有空的

         if(empty($_POST['name'])){
              admin_redirect("分类名称不能为空" );
             die;
         }


        $name = $_POST['name'];
        $pid = $_POST['pid'];
        $path = $_POST['path'];


        //准备sql语句,查询该用户是否已存在
        $sql = "select id from ". PRE . "category where name = '" . $name . "' limit 1";
        // 执行sql语句
        $result = getRow($link, $sql);


        //判断该用户是否已存在
        if($result){
            admin_redirect("该分类已存在");
            die;
        }

        //准备sql语句,向数据库中添加用户
         $sql = "insert into " . PRE . "category (cname, pid, path,create_time) values
    ('".$name."', '".$pid."', '".$path."', '".DATE."')";

        //执行sql语句
        $result = execute($link, $sql);

        if($result){
            admin_redirect("添加分类成功",3,ADMIN_ROOT_URL . "category/index.php");
            die;
        }else{
            admin_redirect("添加分类失败");
            die;
        }

    }elseif($act == 'display'){
        //获取参数
        $id = $_GET['id'];
        $display = $_GET['display'] == 0 ? 1 : 0 ;

        //准备sql
        $sql = "update ". PRE . "category set display=$display where id=$id";

        //执行sql
        $res = execute($link, $sql);
        if($res){
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }else{
            header("location:".$_SERVER["HTTP_REFERER"]);
            die;
        }

    }




?>